package com.boarsoft.boar.oss.service.impl;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.oss.consts.CommonConst;
import com.boarsoft.boar.oss.service.BoarOssClient;
import com.boarsoft.boar.oss.util.DownloadObjectUtil;
import com.boarsoft.boar.oss.util.ManagerObjectUtil;
import com.boarsoft.boar.oss.util.UploadObjectUtil;
import com.boarsoft.common.util.StringUtil;
import com.obs.services.ObsClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

import java.io.File;
import java.io.InputStream;

/**
 * @author tao.wang57
 * @desc 基于华为云OBS的服务实现
 * @date 2023-03-25 am
 */
public class ObsServiceImpl implements BoarOssClient {
    private static final Logger log = LoggerFactory.getLogger(ObsServiceImpl.class);

    private String bucketName;
    private String endpoint;
    @Autowired
    private Environment environment;

    private ObsClient getOssClient(String tenantNo) {
        if (StringUtil.isBlank(tenantNo)) {
            log.error("tenantNo 不允许为空");
        }

        String ak = environment.getProperty(tenantNo.concat(CommonConst.OSS_AK_SUFFIX));
        if (StringUtil.isBlank(ak)) {
            log.error("租户：{}下没有配置对应的 accessKeyId ", tenantNo);
        }

        String sk = environment.getProperty(tenantNo.concat(CommonConst.OSS_SK_SUFFIX));
        if (StringUtil.isBlank(sk)) {
            log.error("租户：{}下没有配置对应的 accessKeySecret ", tenantNo);
        }

        String endpoint = environment.getProperty(tenantNo.concat(CommonConst.OSS_ENDPOINT_SUFFIX));
        this.endpoint = endpoint;
        if (StringUtil.isBlank(endpoint)) {
            log.error("租户：{}下没有配置对应的 endpoint ", tenantNo);
        }

        String bucketName = environment.getProperty(tenantNo.concat(CommonConst.OSS_BUCKETNAME_SUFFIX));
        this.bucketName = bucketName;
        if (StringUtil.isBlank(bucketName)) {
            log.error("租户：{}下没有配置对应的 bucketName ", tenantNo);
        }
        return new ObsClient(ak, sk, endpoint);
    }

    @Override
    public ReplyInfo<Object> downloadFile(String tenantNo, String fileName, String localFolder, String remoteFolder) {
        return DownloadObjectUtil.download(getOssClient(tenantNo), bucketName, remoteFolder + File.separator + fileName, localFolder, fileName);
    }

    @Override
    public InputStream download(String tenantNo, String objectName) {
        return DownloadObjectUtil.download(getOssClient(tenantNo), bucketName, objectName);
    }

    @Override
    public ReplyInfo<Object> uploadFile(String tenantNo, InputStream inputStream, String fileName, String remoteFolder) {
        return UploadObjectUtil.uploadStream(inputStream, getOssClient(tenantNo), bucketName, remoteFolder + File.separator + fileName);
    }

    @Override
    public ReplyInfo<Object> uploadFile(String tenantNo, String fileName, String localFolder, String remoteFolder) {
        return UploadObjectUtil.uploadFile(getOssClient(tenantNo), bucketName, remoteFolder + File.separator + fileName, localFolder + File.separator + fileName);
    }

    @Override
    public ReplyInfo<Object> deleteFile(String tenantNo, String key) {
        return ManagerObjectUtil.deleteByObjectName(getOssClient(tenantNo), bucketName, key);
    }

    @Override
    public ReplyInfo<Object> list(String tenantNo, String bucketName) {
        return ManagerObjectUtil.listSimple(getOssClient(tenantNo), bucketName);
    }
}
